package com.gxuwz.stats.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxuwz.stats.entity.DecisionConsulting;
import com.gxuwz.stats.entity.User;
import com.gxuwz.stats.mapper.DecisionConsultingMapper;
import com.gxuwz.stats.service.DecisionConsultingService;
import com.gxuwz.stats.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DecisionConsultingServiceImpl extends ServiceImpl<DecisionConsultingMapper, DecisionConsulting>
        implements DecisionConsultingService {

    @Autowired
    private UserService userService;

    public IPage<DecisionConsulting> getDecisionConsultingList(IPage<DecisionConsulting>  page,QueryWrapper<DecisionConsulting> queryWrapper){
        IPage<DecisionConsulting> resultPage = this.page(page,queryWrapper);
        for(DecisionConsulting decisionConsulting : resultPage.getRecords()){
            Integer userId = decisionConsulting.getUserId();
            if(userId != null){
                User user = userService.getById(userId);
                if(user != null){
                    decisionConsulting.setUsername(user.getUsername());
                }
            }
        }
        return  resultPage;
    }

    @Override
    public IPage<DecisionConsulting> page(Page<DecisionConsulting> page, QueryWrapper<DecisionConsulting> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<DecisionConsulting> list(QueryWrapper<DecisionConsulting> queryWrapper) {
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean save(DecisionConsulting decisionConsulting) {
        return baseMapper.insertWithUserId(decisionConsulting) > 0;
    }

    @Override
    public boolean removeById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        QueryWrapper<DecisionConsulting> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("consulting_id", ids);
        return baseMapper.delete(queryWrapper) > 0;
    }

    @Override
    public DecisionConsulting getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean updateById(DecisionConsulting decisionConsulting) {
        return baseMapper.updateById(decisionConsulting) > 0;
    }
}
